using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
namespace SilverSea.Video.Tools
{
	public class DefineRegionsControl : Control
	{
		private IContainer icontainer_0;
		private System.Drawing.Color color_0 = System.Drawing.Color.Black;
		private System.Drawing.Color color_1 = System.Drawing.Color.FromArgb(96, 96, 96);
		private System.Drawing.Color color_2 = System.Drawing.Color.Yellow;
		private System.Drawing.Color color_3 = System.Drawing.Color.FromArgb(0, 255, 0);
		private System.Drawing.Bitmap bitmap_0;
		private List<System.Drawing.Rectangle> list_0 = new List<System.Drawing.Rectangle>();
		private DrawingMode drawingMode_0;
		private bool bool_0;
		private System.Drawing.Point point_0;
		private System.Drawing.Point point_1;
		private NewRectangleHandler newRectangleHandler_0;
		public event NewRectangleHandler OnNewRectangle
		{
			add
			{
				NewRectangleHandler newRectangleHandler = this.newRectangleHandler_0;
				NewRectangleHandler newRectangleHandler2;
				do
				{
					newRectangleHandler2 = newRectangleHandler;
					NewRectangleHandler value2 = (NewRectangleHandler)Delegate.Combine(newRectangleHandler2, value);
					newRectangleHandler = Interlocked.CompareExchange<NewRectangleHandler>(ref this.newRectangleHandler_0, value2, newRectangleHandler2);
				}
				while (newRectangleHandler != newRectangleHandler2);
			}
			remove
			{
				NewRectangleHandler newRectangleHandler = this.newRectangleHandler_0;
				NewRectangleHandler newRectangleHandler2;
				do
				{
					newRectangleHandler2 = newRectangleHandler;
					NewRectangleHandler value2 = (NewRectangleHandler)Delegate.Remove(newRectangleHandler2, value);
					newRectangleHandler = Interlocked.CompareExchange<NewRectangleHandler>(ref this.newRectangleHandler_0, value2, newRectangleHandler2);
				}
				while (newRectangleHandler != newRectangleHandler2);
			}
		}
		public new System.Drawing.Bitmap BackgroundImage
		{
			get
			{
				return this.bitmap_0;
			}
			set
			{
				this.bitmap_0 = value;
			}
		}
		public DrawingMode DrawingMode
		{
			get
			{
				return this.drawingMode_0;
			}
			set
			{
				this.drawingMode_0 = value;
				this.Cursor = ((this.drawingMode_0 == DrawingMode.None) ? Cursors.Default : Cursors.Cross);
			}
		}
		public System.Drawing.Rectangle[] Rectangles
		{
			get
			{
				System.Drawing.Rectangle[] array = new System.Drawing.Rectangle[this.list_0.Count];
				this.list_0.CopyTo(array);
				return array;
			}
			set
			{
				this.list_0.Clear();
				if (value != null)
				{
					this.list_0.AddRange(value);
				}
				base.Invalidate();
			}
		}
		public DefineRegionsControl()
		{
			this.method_0();
			base.SetStyle(ControlStyles.UserPaint | ControlStyles.ResizeRedraw | ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer, true);
		}
		protected override void Dispose(bool disposing)
		{
			if (disposing && this.icontainer_0 != null)
			{
				this.icontainer_0.Dispose();
			}
			base.Dispose(disposing);
		}
		private void method_0()
		{
			this.icontainer_0 = new Container();
		}
		public void RemoveAllRegions()
		{
			this.list_0.RemoveRange(0, this.list_0.Count);
			base.Invalidate();
		}
		protected override void OnPaint(PaintEventArgs pe)
		{
			System.Drawing.Graphics graphics = pe.Graphics;
			System.Drawing.Rectangle clientRectangle = base.ClientRectangle;
			using (System.Drawing.Pen pen = new System.Drawing.Pen(this.color_0, 1f))
			{
				graphics.DrawRectangle(pen, clientRectangle.X, clientRectangle.Y, clientRectangle.Width - 1, clientRectangle.Height - 1);
			}
			if (this.bitmap_0 != null)
			{
				graphics.DrawImage(this.bitmap_0, 1, 1, clientRectangle.Width - 2, clientRectangle.Height - 2);
			}
			else
			{
				using (System.Drawing.Brush brush = new System.Drawing.SolidBrush(this.color_1))
				{
					graphics.FillRectangle(brush, 1, 1, clientRectangle.Width - 2, clientRectangle.Height - 2);
				}
			}
			using (System.Drawing.Pen pen2 = new System.Drawing.Pen(this.color_3, 1f))
			{
				foreach (System.Drawing.Rectangle current in this.list_0)
				{
					graphics.DrawRectangle(pen2, current.X + 1, current.Y + 1, current.Width - 1, current.Height - 1);
				}
			}
			base.OnPaint(pe);
		}
		protected override void OnMouseDown(MouseEventArgs e)
		{
			if (this.drawingMode_0 == DrawingMode.Rectangular)
			{
				if (e.Button == MouseButtons.Left)
				{
					this.bool_0 = true;
					base.Capture = true;
					this.point_0.X = (this.point_1.X = e.X);
					this.point_0.Y = (this.point_1.Y = e.Y);
					ControlPaint.DrawReversibleFrame(new System.Drawing.Rectangle(e.X, e.Y, 1, 1), System.Drawing.Color.Green, FrameStyle.Dashed);
					return;
				}
				MouseButtons arg_87_0 = e.Button;
			}
		}
		protected override void OnMouseUp(MouseEventArgs e)
		{
			if (this.drawingMode_0 == DrawingMode.Rectangular && this.bool_0)
			{
				this.bool_0 = false;
				base.Capture = false;
				this.drawingMode_0 = DrawingMode.None;
				this.Cursor = Cursors.Default;
				this.method_1();
				this.method_2(ref this.point_0, ref this.point_1);
				this.method_3(ref this.point_0);
				this.method_3(ref this.point_1);
				System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(this.point_0.X - 1, this.point_0.Y - 1, this.point_1.X - this.point_0.X + 1, this.point_1.Y - this.point_0.Y + 1);
				this.list_0.Add(rectangle);
				if (this.newRectangleHandler_0 != null)
				{
					this.newRectangleHandler_0(this, rectangle);
				}
				base.Invalidate();
			}
		}
		protected override void OnMouseMove(MouseEventArgs e)
		{
			if (this.bool_0)
			{
				this.method_1();
				this.point_1.X = e.X;
				this.point_1.Y = e.Y;
				this.method_1();
			}
		}
		private void method_1()
		{
			System.Drawing.Point p = this.point_0;
			System.Drawing.Point p2 = this.point_1;
			this.method_2(ref p, ref p2);
			this.method_3(ref p);
			this.method_3(ref p2);
			System.Drawing.Point point = base.PointToScreen(p);
			System.Drawing.Point point2 = base.PointToScreen(p2);
			ControlPaint.DrawReversibleFrame(new System.Drawing.Rectangle(point.X, point.Y, point2.X - point.X + 1, point2.Y - point.Y + 1), this.color_2, FrameStyle.Dashed);
		}
		private void method_2(ref System.Drawing.Point point_2, ref System.Drawing.Point point_3)
		{
			System.Drawing.Point point = point_2;
			System.Drawing.Point point2 = point_3;
			point_2.X = Math.Min(point.X, point2.X);
			point_2.Y = Math.Min(point.Y, point2.Y);
			point_3.X = Math.Max(point.X, point2.X);
			point_3.Y = Math.Max(point.Y, point2.Y);
		}
		private void method_3(ref System.Drawing.Point point_2)
		{
			if (point_2.X < 1)
			{
				point_2.X = 1;
			}
			if (point_2.Y < 1)
			{
				point_2.Y = 1;
			}
			if (point_2.X >= base.ClientRectangle.Width - 1)
			{
				point_2.X = base.ClientRectangle.Width - 2;
			}
			if (point_2.Y >= base.ClientRectangle.Height - 1)
			{
				point_2.Y = base.ClientRectangle.Height - 2;
			}
		}
	}
}
